﻿@model Dictionary<string, System.Collections.Generic.List<CoolCat.Core.DomainModel.QueryDocumentItem>>
@{
    ViewData["Title"] = "Documentation";
}

<h1>Module Query Documentation</h1>

@foreach (var item in Model)
{
    <div class="document-group">
        <div class="document-title">@item.Key</div>

        @foreach (var d in item.Value)
        {
            <div class="documentItem">
                <div class="document-query-name">@d.QueryName</div>
                <div class="document-sample">
                    <div class="document-request-sample">
                        <div class="document-query-sample-title">Request Sample:</div>
                        <div class="sample-container">
                            <textarea cols="100" rows="6" class="sample-content" readonly>@Html.Raw(d.RequestSample)</textarea>
                        </div>
                    </div>
                    <div class="document-response-sample">
                        <div class="document-query-sample-title">Response Sample:</div>
                        <div class="sample-container">
                            <textarea cols="100" rows="6" class="sample-content" readonly>@Html.Raw(@d.ResponseSample)</textarea>
                        </div>
                    </div>
                    <div class="document-response-sample">
                        <div class="document-query-sample-title">Calling Sample:</div>
                        <div class="sample-container">
                            <textarea cols="100" rows="6" class="sample-content" readonly>_dataStore.Query("@item.Key", "@d.QueryName", @@requestParam, source: ModuleDefiniation.MODULE_NAME)</textarea>
                        </div>
                    </div>
                </div>

            </div>
        }
    </div>
}

@section Scripts{

    <script type="text/javascript">

        function isJSON(str) {
            if (typeof str == 'string') {
                try {
                    var obj = JSON.parse(str);
                    if (typeof obj == 'object' && obj) {
                        return true;
                    } else {
                        return false;
                    }

                } catch (e) {
                    console.log('error：' + str + '!!!' + e);
                    return false;
                }
            }
            console.log('It is not a string!')
        }

        $(document).ready(function () {

            var textAreas = $('.sample-content');

            textAreas.each(function () {

                if ($(this).val() != "") {

                    var val = $(this).val();

                    if (isJSON(val)) {
                        var obj = JSON.parse(val);
                        var o = JSON.stringify(obj, null, 4);

                        $(this).val(o);
                    }
                }


            });


        });
    </script>

}